from pymouse import PyMouse
import time
from threading import Thread
import wx
import json
import os

class TestThread(Thread):
 def __init__(self):
  Thread.__init__(self)

 def run(self):
  time.sleep(5)
  i = 0
  while True:
      if not flag:
          return
      if time0<=0 or time1<=0 or time2<=0:
          content_text.AppendText("此地图数据无效\n")
          timer.Stop()
          return
      i = i + 1
      count_text.SetValue("%d" %(i))
      content_text.AppendText('第%d次游戏执行\n' % (i))
      m.click(oneFrame[0], oneFrame[1])  # 第一层开始行动
      time.sleep(time0)
      m.click(twoFrame[0], twoFrame[1])  # 第二层开始行动
      content_text.AppendText("游戏进行中\n")
      time.sleep(time1)
      content_text.AppendText("本局结束\n")
      m.click(threeFrame[0], threeFrame[1])  # 结束
      time.sleep(time2)

m = PyMouse()
a = m.position()    #获取当前坐标的位置
print(a)
flag = False#开始结束开关
seconds = 0#毫秒值

mapNameList = []#地图名称
mapDataJson = []#地图全部数据
mapValue = ""#地图名称
time0=0
time1=0
time2=0
oneFrame = [0,0]
twoFrame = [0,0]
threeFrame = [0,0]
#开始
def start(event):
    global flag
    flag = True
    content_text.AppendText("开始执行脚本\n")
    thread = TestThread()
    thread.start()
    timer.Start(1000)


#结束
def over(event):
    global flag
    global text
    flag = False
    content_text.AppendText("结束执行脚本\n")
    timer.Stop()
#时间显示
def OnTimer(self):#显示时间事件处理函数
    global seconds
    seconds = seconds + 1
    m, s = divmod(seconds, 60)
    h, m = divmod(m, 60)
    time_text.SetValue("%d:%02d:%02d" % (h, m, s))#显示时间

#地图栏内容展示
def on_combobox(event):
    listbox = event.GetEventObject()
    index = int(format(listbox.GetSelection()))
    global mapValue
    mapValue = mapNameList[index]
    map_text.SetValue("%s" %(mapValue))
    global time0,time1,time2
    #遍历地图数据拿到每个当前选择的数据
    for mapData in mapDataJson:
        if mapData['name']==mapValue :
            time0 = mapData['timeOne']
            time1 = mapData['timeTwo']
            time2 = mapData['timeThree']

#游戏数据读取
def loadData():
    createFile("mapData.json")
    f = open("mapData.json", encoding='utf-8') # 设置以utf - 8
    mapDataList = json.load(f)
    global mapDataJson
    mapDataJson = mapDataList
    for mapData in mapDataList:
        mapNameList.append(mapData['name'])
    f.close()

#创建文件
def createFile(fileName):
    if not os.path.isfile(fileName):  # 无文件时创建
        jsonData = [{"name": "ceshi","timeOne": 0,"timeTwo": 0,"timeThree": 0}]
        json_str = json.dumps(jsonData, indent=4)
        with open('mapData.json', 'w') as json_file:
            json_file.write(json_str)
#添加
def add(event):
    global dialog
    global map_name_text
    global time0_text
    global time1_text
    global time2_text
    dialog = wx.Dialog(frame,title="添加地图")
    wx.StaticText(dialog, -1, u'地图名称：', pos=(5, 8), size=(90, -1), style=wx.ALIGN_RIGHT)
    map_name_text = wx.TextCtrl(dialog, -1, '', pos=(100, 5), size=(100, -1), name='TC01', style=wx.TE_CENTER)

    wx.StaticText(dialog, -1, u'时间1：', pos=(5, 38), size=(90, -1), style=wx.ALIGN_RIGHT)
    time0_text = wx.TextCtrl(dialog, -1, '', pos=(100, 35), size=(100, -1), name='TC01', style=wx.TE_CENTER)

    wx.StaticText(dialog, -1, u'时间2：', pos=(5, 68), size=(90, -1), style=wx.ALIGN_RIGHT)
    time1_text = wx.TextCtrl(dialog, -1, '', pos=(100, 65), size=(100, -1), name='TC01', style=wx.TE_CENTER)

    wx.StaticText(dialog, -1, u'时间3：', pos=(5, 98), size=(90, -1), style=wx.ALIGN_RIGHT)
    time2_text = wx.TextCtrl(dialog, -1, '', pos=(100, 95), size=(100, -1), name='TC01', style=wx.TE_CENTER)
    sava_button = wx.Button(dialog, label="保存", pos=(100, 130), size=(50, -1))
    sava_button.Bind(wx.EVT_BUTTON, sava)  # 绑定开始事件
    rec = dialog.ShowModal()
def update(event):
    pass
def delete(event):
    global delete_dialog
    delete_dialog = wx.Dialog(frame,title="删除地图",size=(300,150))
    text = "你确定要删除"+mapValue
    wx.StaticText(delete_dialog, -1, text, pos=(5, 8), size=(100, -1), style=wx.ALIGN_RIGHT)
    delete_button = wx.Button(delete_dialog, label="确定", pos=(100, 40), size=(50, -1))
    break_button = wx.Button(delete_dialog, label="取消", pos=(160, 40), size=(50, -1))
    delete_button.Bind(wx.EVT_BUTTON, deleteByName)  # 绑定删除事件
    break_button.Bind(wx.EVT_BUTTON, breakB)  # 绑定取消事件
    rec = delete_dialog.ShowModal()
#取消关闭窗口
def breakB(event):
    delete_dialog.Destroy()
#删除
def deleteByName(event):
    # 遍历地图数据拿到每个当前选择的数据
    count = 0
    for mapData in mapDataJson:
        if mapData['name'] == mapValue:
            break
        count = count+1
    # 执行删除操作
    del mapDataJson[count]
    json_str = json.dumps(mapDataJson, indent=4)
    with open('mapData.json', 'w') as json_file:
        json_file.write(json_str)
    delete_dialog.Destroy()
#添加操作
def sava(event):
    appJson = {}
    name = map_name_text.GetValue()
    timeOne = time0_text.GetValue()
    timeTwo = time1_text.GetValue()
    timeThree = time2_text.GetValue()
    if name=="" or timeOne=="" or timeTwo=="" or timeThree=="":
        return
    appJson['name'] = name
    appJson['timeOne'] = int(timeOne)
    appJson['timeTwo'] = int(timeTwo)
    appJson['timeThree'] = int(timeThree)
    if len(appJson)<=0:
        return
    mapDataJson.append(appJson)
    json_str = json.dumps(mapDataJson,indent=4)
    with open('mapData.json', 'w') as json_file:
        json_file.write(json_str)
    loadData()
    lb1.Update()
    dialog.Destroy()
def loadZbData():
    global oneFrame
    global twoFrame
    global threeFrame
    if not os.path.isfile("zbData.json"):  # 无文件时创建
        jsonData = [[1239,737],[1217,591],[990,575]]
        json_str = json.dumps(jsonData, indent=4)
        with open('zbData.json', 'w') as json_file:
            json_file.write(json_str)
    f = open("zbData.json", encoding='utf-8')  # 设置以utf - 8
    zbDataList = json.load(f)
    f.close()
    oneFrame = zbDataList[0]
    twoFrame = zbDataList[1]
    threeFrame = zbDataList[2]
#窗口创建
app = wx.App() #实例化一个主循环
frame = wx.Frame(None,title = "明日方舟自动化脚本",size = (500,400)) #实例化一个窗口
content_text= wx.TextCtrl(frame,pos = (5,100),size = (475,250),style = wx.TE_MULTILINE | wx.TE_READONLY)
start_button = wx.Button(frame,label = "开始",pos = (5,5),size = (100,40))
over_button = wx.Button(frame,label = "结束",pos = (5,50),size = (100,40))

start_button.Bind(wx.EVT_BUTTON,start)#绑定开始事件
over_button.Bind(wx.EVT_BUTTON,over)#绑定结束事件

add_button = wx.Button(frame,label = "添加",pos = (420,5),size = (50,-1))
update_button = wx.Button(frame,label = "修改",pos = (420,35),size = (50,-1))
delete_button = wx.Button(frame,label = "删除",pos = (420,65),size = (50,-1))

add_button.Bind(wx.EVT_BUTTON,add)#绑定添加事件
update_button.Bind(wx.EVT_BUTTON,update)#绑定修改事件
delete_button.Bind(wx.EVT_BUTTON,delete)#绑定删除事件

wx.StaticText(frame, -1, u'进图次数：', pos=(110, 8), size=(90, -1), style=wx.ALIGN_RIGHT )
count_text = wx.TextCtrl(frame, -1, '', pos=(200, 5), size=(100, -1), name='TC01', style=wx.TE_CENTER | wx.TE_READONLY)

wx.StaticText(frame, -1, u'执行时长：', pos=(110, 38), size=(90, -1), style=wx.ALIGN_RIGHT )
time_text = wx.TextCtrl(frame, -1, '', pos=(200, 35), size=(100, -1), name='TC02', style=wx.TE_CENTER | wx.TE_READONLY)

wx.StaticText(frame, -1, u'当前地图：', pos=(110, 68), size=(90, -1), style=wx.ALIGN_RIGHT)
map_text = wx.TextCtrl(frame, -1, '', pos=(200, 65), size=(100, -1), name='TC03', style=wx.TE_CENTER | wx.TE_READONLY)

# 创建定时器
timer = wx.Timer()  # 创建定时器
timer.Bind(wx.EVT_TIMER, OnTimer)  # 绑定一个定时器事件

loadData()
loadZbData()
#地图列表选择
lb1=wx.ListBox(frame,-1,pos=(310, 8), size=(100, 80),choices=mapNameList,style=wx.LB_SINGLE)
#添加事件处理
lb1.Bind(wx.EVT_LISTBOX,on_combobox)

frame.Show()#调用窗口展示功能
app.MainLoop()#启动主循环
